<?php
/***************************************************************************
 *				   admin_groups.php
 *				  -------------------
 *	 begin		  : Saturday, Feb 13, 2001
 *	 copyright		  : (C) 2001 The phpBB Group
 *	 email		  : support@phpbb.com
 *
 ***************************************************************************/
/***************************************************************************
* phpbb2 forums port version 2.0.5 (c) 2003 - Nuke Cops (http://nukecops.com)
*
* Ported by Nuke Cops to phpbb2 standalone 2.0.5 Test
* and debugging completed by the Elite Nukers and site members.
*
* You run this package at your sole risk. Nuke Cops and affiliates cannot
* be held liable if anything goes wrong. You are advised to test this
* package on a development system. Backup everything before implementing
* in a production environment. If something goes wrong, you can always
* backout and restore your backups.
*
* Installing and running this also means you agree to the terms of the AUP
* found at Nuke Cops.
*
* This is version 2.0.5 of the phpbb2 forum port for PHP-Nuke. Work is based
* on Tom Nitzschner's forum port version 2.0.6. Tom's 2.0.6 port was based
* on the phpbb2 standalone version 2.0.3. Our version 2.0.5 from Nuke Cops is
* now reflecting phpbb2 standalone 2.0.5 that fixes some bugs and the
* invalid_session error message.
***************************************************************************/
/***************************************************************************
 *	 This file is part of the phpBB2 port to Nuke 6.0 (c) copyright 2002
 *	 by Tom Nitzschner (tom@toms-home.com)
 *	 http://bbtonuke.sourceforge.net (or http://www.toms-home.com)
 *
 *	 As always, make a backup before messing with anything. All code
 *	 release by me is considered sample code only. It may be fully
 *	 functual, but you use it at your own risk, if you break it,
 *	 you get to fix it too. No waranty is given or implied.
 *
 *	 Please post all questions/request about this port on http://bbtonuke.sourceforge.net first,
 *	 then on my site. All original header code and copyright messages will be maintained
 *	 to give credit where credit is due. If you modify this, the only requirement is
 *	 that you also maintain all original copyright messages. All my work is released
 *	 under the GNU GENERAL PUBLIC LICENSE. Please see the README for more information.
 *
 ***************************************************************************/
/***************************************************************************
 *
 *	 This program is free software; you can redistribute it and/or modify
 *	 it under the terms of the GNU General Public License as published by
 *	 the Free Software Foundation; either version 2 of the License, or
 *	 (at your option) any later version.
 *
 ***************************************************************************/
if (!defined('ADMIN_PAGES')) { exit; }

if (isset($_POST['g']))
{
	URL::redirect(URL::admin('&do=groups&g='.$_POST->uint('g')));
	exit;
}

$group_id = $_GET->uint('g');

if ($group_id && isset($_POST['group_update'])) {
	//
	// Ok, they are submitting a group, let's save the data based on if it's new or editing
	//
	if (isset($_POST['group_delete'])) {
		//
		// Reset User Moderator Level
		//

		// Is Group moderating a forum ?
		$row = $db->uFetchRow("SELECT auth_mod FROM " . AUTH_ACCESS_TABLE . " WHERE group_id = " . $group_id);
		if (intval($row[0]) == 1)
		{
			// Yes, get the assigned users and update their Permission if they are no longer moderator of one of the forums
			$result = $db->query("SELECT user_id FROM " . USER_GROUP_TABLE . " WHERE group_id = " . $group_id);
			while ($row = $result->fetch_row())
			{
				$sql = "SELECT g.group_id FROM " . AUTH_ACCESS_TABLE . " a, " . GROUPS_TABLE . " g, " . USER_GROUP_TABLE . " ug
				WHERE (a.auth_mod = 1) AND (g.group_id = a.group_id) AND (a.group_id = ug.group_id) AND (g.group_id = ug.group_id)
					AND (ug.user_id = " . intval($row[0]) . ") AND (ug.group_id <> " . $group_id . ")";
				if ( !($result = $db->query($sql)) )
				{
					message_die(GENERAL_ERROR, 'Could not obtain moderator permissions', '', __LINE__, __FILE__, $sql);
				}

				if (!$result->num_rows) {
					$db->query("UPDATE " . USERS_TABLE . " SET user_level = " . USER . " WHERE user_level = " . MOD . " AND user_id = " . intval($row[0]));
				}
			}
		}

		//
		// Delete Group
		//
		$db->query("DELETE FROM " . GROUPS_TABLE . " WHERE group_id = " . $group_id);
		$db->query("DELETE FROM " . USER_GROUP_TABLE . " WHERE group_id = " . $group_id);
		$db->query("DELETE FROM " . AUTH_ACCESS_TABLE . " WHERE group_id = " . $group_id);
		$db->query("DELETE FROM " . QUOTA_TABLE . " WHERE group_id = {$group_id} AND quota_type = " . QUOTA_UPLOAD_LIMIT);

		$message = $lang['Deleted_group'] . '<br /><br />' . sprintf($lang['Click_return_groupsadmin'], '<a href="'.URL::admin("&amp;do=groups").'">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="'.URL::admin("forums").'">', '</a>');
		message_die(GENERAL_MESSAGE, $message);
	}
	else
	{
		# Get the contents
		$quota_limit_id = $_POST->uint('group_upload_quota');
		if ($quota_limit_id) {
			$result = $db->query("SELECT group_id FROM " . QUOTA_TABLE . " WHERE group_id = {$group_id} AND quota_type = " . QUOTA_UPLOAD_LIMIT);
			if ($result->num_rows) {
				$db->query("UPDATE " . QUOTA_TABLE . " SET quota_limit_id = " . $quota_limit_id . " WHERE group_id = {$group_id} AND quota_type = " . QUOTA_UPLOAD_LIMIT);
			} else {
				$db->query("INSERT INTO " . QUOTA_TABLE . " (user_id, group_id, quota_type, quota_limit_id)
				VALUES (0, {$group_id}, " . QUOTA_UPLOAD_LIMIT . ", {$quota_limit_id})");
			}
		} else {
			$db->query("DELETE FROM " . QUOTA_TABLE . " WHERE group_id = {$group_id} AND quota_type = " . QUOTA_UPLOAD_LIMIT);
		}

		$group_type = isset($_POST['group_type']) ? intval($_POST['group_type']) : GROUP_OPEN;
		$group_name = isset($_POST['group_name']) ? trim($_POST['group_name']) : '';
		$group_description = isset($_POST['group_description']) ? trim($_POST['group_description']) : '';
		$group_moderator = isset($_POST['username']) ? $_POST['username'] : '';
		$delete_old_moderator = isset($_POST['delete_old_moderator']);

		if ( $group_name == '' ) {
			message_die(GENERAL_MESSAGE, $lang['No_group_name']);
		} else if ( $group_moderator == '' ) {
			message_die(GENERAL_MESSAGE, $lang['No_group_moderator']);
		}

		$this_userdata = getusrdata($group_moderator, true);
		$group_moderator = $this_userdata['user_id'];

		if ( !$group_moderator ) {
			message_die(GENERAL_MESSAGE, $lang['No_group_moderator']);
		}

		$sql = "SELECT * FROM " . GROUPS_TABLE . "
			WHERE group_single_user <> 1
			AND group_id = " . $group_id;
		if ( !($result = $db->query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Error getting group information', '', __LINE__, __FILE__, $sql);
		}

		if( !($group_info = $result->fetch_assoc()) )
		{
			message_die(GENERAL_MESSAGE, $lang['Group_not_exist']);
		}

		if ( $group_info['group_moderator'] != $group_moderator )
		{
			if ( $delete_old_moderator )
			{
				$sql = "DELETE FROM " . USER_GROUP_TABLE . "
					WHERE user_id = " . $group_info['group_moderator'] . "
						AND group_id = " . $group_id;
				if ( !$db->query($sql) )
				{
					message_die(GENERAL_ERROR, 'Could not update group moderator', '', __LINE__, __FILE__, $sql);
				}
			}

			$sql = "SELECT user_id
				FROM " . USER_GROUP_TABLE . "
				WHERE user_id = $group_moderator
					AND group_id = $group_id";
			if ( !($result = $db->query($sql)) )
			{
				message_die(GENERAL_ERROR, 'Failed to obtain current group moderator info', '', __LINE__, __FILE__, $sql);
			}

			if (!$result->fetch_row())
			{
				$sql = "INSERT INTO " . USER_GROUP_TABLE . " (group_id, user_id, user_pending)
					VALUES (" . $group_id . ", " . $group_moderator . ", 0)";
				if ( !$db->query($sql) )
				{
					message_die(GENERAL_ERROR, 'Could not update group moderator', '', __LINE__, __FILE__, $sql);
				}
			}
		}

		$sql = "UPDATE " . GROUPS_TABLE . "
			SET group_type = $group_type, group_name = '" . Fix_Quotes($group_name) . "', group_description = '" . Fix_Quotes($group_description) . "', group_moderator = $group_moderator
			WHERE group_id = $group_id";
		if ( !$db->query($sql) )
		{
			message_die(GENERAL_ERROR, 'Could not update group', '', __LINE__, __FILE__, $sql);
		}

		$message = $lang['Updated_group'] . '<br /><br />' . sprintf($lang['Click_return_groupsadmin'], '<a href="'.URL::admin("&amp;do=groups").'">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="'.URL::admin("forums").'">', '</a>');;

		message_die(GENERAL_MESSAGE, $message);
	}
}

elseif (isset($_GET['g']))
{
	$template->set_filenames(array('body' => 'admin/forums/group_edit'));

	$group = $db->uFetchAssoc("SELECT
		group_id          id,
		group_type        type,
		group_name        name,
		group_description description,
		group_moderator   moderator
	FROM " . GROUPS_TABLE . "
	WHERE group_id = {$group_id}
	  AND group_single_user <> 1");
	if (!$group) {
		message_die(GENERAL_MESSAGE, $lang['Group_not_exist']);
	}

	$group_moderator = $db->uFetchRow("SELECT username FROM " . USERS_TABLE . " WHERE user_id = {$group['moderator']}");
	$group['moderator'] = $group_moderator ? $group_moderator[0] : '';

	include('includes/phpBB/attach/functions_selects.php');
	$upload_quota = $db->uFetchRow("SELECT quota_limit_id FROM " . QUOTA_TABLE . " WHERE group_id={$group_id} AND quota_type=".QUOTA_UPLOAD_LIMIT);

	$template->group = $group;
	$template->assign_vars(array(
		'U_SEARCH_USER' => URL::index("&file=search&mode=searchuser&popup=1&menu=1", true, true),
		'S_GROUP_OPEN_CHECKED'   => $group['type'] == GROUP_OPEN,
		'S_GROUP_CLOSED_CHECKED' => $group['type'] == GROUP_CLOSED,
		'S_GROUP_HIDDEN_CHECKED' => $group['type'] == GROUP_HIDDEN,
		'S_SELECT_UPLOAD_QUOTA'  => quota_limit_select('group_upload_quota', $upload_quota ? $upload_quota[0] : -1),
	));
}

else
{
	$template->forum_groups = $db->query("SELECT
		group_id id,
		group_name name
	FROM " . GROUPS_TABLE . "
	WHERE group_single_user <> 1
	ORDER BY group_name");
	$template->set_filenames(array('body' => 'admin/forums/group_select'));
}
